package com.newtouch.bxzs.business.statistics.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.business.product.controller.ProductController;
import com.newtouch.bxzs.business.statistics.mapper.ActivityAchievementMapper;
import com.newtouch.bxzs.business.statistics.model.bo.*;
import com.newtouch.bxzs.business.statistics.model.vo.ActivityAchievementListVO;
import com.newtouch.bxzs.business.statistics.model.vo.ActivityAchievementVO;
import com.newtouch.bxzs.business.statistics.model.vo.ActivityProductSaleRankingVO;
import com.newtouch.bxzs.business.statistics.service.ActivityAchievementService;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 活动期业绩实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/3/3 9:31</td>
 * <td>tianshangzhu</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author tianshangzhu
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class ActivityAchievementImpl implements ActivityAchievementService {

    @Resource
    private ActivityAchievementMapper activityAchievementMapper;

    /**
     * 排名前十
     */
    private final Integer RANK = 10;

    /**
     * 静态变量：系统日志
     */
    private static final Log logger = LogFactory.getLog(ProductController.class);

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.ActivityAchievementImpl#
     * getActivityAchievementSummary(com.newtouch.bxzs.business.statistics.model.vo.ActivityAchievementSummaryVO)
     */
    @Override
    public ActivityAchievementSummaryBO getActivityAchievementSummary(ActivityAchievementVO activityAchievementVO) {
        //查询活动期个人业绩信息汇总
        //效验
        if (BeanUtil.isEmpty(activityAchievementVO)) {
            logger.info("传入参数为空");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        if (StringUtils.isEmpty(activityAchievementVO.getActivityId())) {
            logger.info("必填项为空");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        if (StringUtils.isEmpty(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserJobNumber())) {
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        } else {
            activityAchievementVO.setUserJobNumber(UserDetailUtil.getUserDetail().getUserJobNumber());
        }
        return activityAchievementMapper.getActivityAchievementSummary(activityAchievementVO);
    }

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.ActivityAchievementImpl#
     * getActivityAchievementPolicyList(com.newtouch.bxzs.business.statistics.model.vo.ActivityAchievementVO)
     */
    @Override
    public ActivityAchievementPolicyListBO getActivityAchievementPolicyList(ActivityAchievementListVO activityAchievementListVO) {
        //效验
        if (BeanUtil.isEmpty(activityAchievementListVO)) {
            logger.info("传入参数为空");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        if (StringUtils.isEmpty(activityAchievementListVO.getActivityId()) ||
                activityAchievementListVO.getCurrentPage() == null || activityAchievementListVO.getPageSize() == null) {
            logger.info("必填项为空");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        if (StringUtils.isEmpty(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserJobNumber())) {
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        } else {
            activityAchievementListVO.setUserJobNumber(UserDetailUtil.getUserDetail().getUserJobNumber());
        }
        //查看活动期个人业绩保单列表
        Page<ActivityAchievementPolicyBO> page = new Page<>(activityAchievementListVO.getCurrentPage(), activityAchievementListVO.getPageSize());
        //实例化返回值
        ActivityAchievementPolicyListBO activityAchievementPolicyListBO = new ActivityAchievementPolicyListBO();
        //设置保单列表数据
        activityAchievementPolicyListBO.setActivityAchievementPolicyBOList(activityAchievementMapper.getActivityAchievementPolicyList(page, activityAchievementListVO));
        //设置活动ID
        activityAchievementPolicyListBO.setActivityCode(activityAchievementListVO.getActivityId());
        //根据营销活动Id查询活动主题
        activityAchievementPolicyListBO.setActivityTheme(activityAchievementMapper.getActivityThemeById(activityAchievementListVO.getActivityId()));
        return activityAchievementPolicyListBO;
    }

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.ActivityAchievementImpl#
     * getActivityAchievementDetail(java.lang.String)
     */
    @Override
    public AchievementPolicyDetailBO getActivityAchievementDetail(String policyNumber) {
        //查看活动期个人业绩详情
        /* 校验 */
        if (StringUtils.isEmpty(policyNumber)) {
            logger.info("传入参数为空");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        return activityAchievementMapper.getActivityAchievementDetail(policyNumber);
    }

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.ActivityAchievementImpl#
     * getActivityAchievementRanking(com.newtouch.bxzs.business.statistics.model.vo.ActivityAchievementVO)
     */
    @Override
    public ActivityAchievementRankingListBO getActivityAchievementRanking(ActivityAchievementVO activityAchievementVO) {
        //效验
        if (BeanUtil.isEmpty(activityAchievementVO)) {
            logger.info("传入参数为空");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        if (StringUtils.isEmpty(activityAchievementVO.getActivityId())) {
            logger.info("必填项为空");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        if (StringUtils.isEmpty(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserJobNumber())) {
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        } else {
            activityAchievementVO.setUserJobNumber(UserDetailUtil.getUserDetail().getUserJobNumber());
        }
        //查看活动期个人业绩排名
        ActivityAchievementRankingListBO activityAchievementRankingListBO = new ActivityAchievementRankingListBO();
        //获取机构编码
        String organizationCodes = activityAchievementMapper.getOrganizationCode(activityAchievementVO.getUserJobNumber());
        if (StringUtils.isEmpty(organizationCodes)) {
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        }
        String[] organizationCode = organizationCodes.substring(1).split(",");
        //设置个人业绩
        BeanUtil.copyProperties(activityAchievementMapper.getActivityPersonalRanking(activityAchievementVO, organizationCode, RANK), activityAchievementRankingListBO);
        // 获取当前登录用户的用户工号
        activityAchievementRankingListBO.setUserJobNumber(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserJobNumber());
        // 获取当前登录用户的用户姓名
        activityAchievementRankingListBO.setUserName(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserName());
        //设置业绩排名列表
        activityAchievementRankingListBO.setList(activityAchievementMapper.getActivityAchievementRanking(activityAchievementVO, organizationCode, RANK));
        return activityAchievementRankingListBO;
    }

    /**
     * (non-Javadoc)
     * <p>
     * <p>
     * com.newtouch.bxzs.business.statistics.service.impl.ActivityAchievementImpl#
     * getActivityProductSaleRanking(com.newtouch.bxzs.business.statistics.model.vo.ActivityProductSaleRankingVO)
     */
    @Override
    public ActivityProductSaleRankingListBO getActivityProductSaleRanking(ActivityProductSaleRankingVO activityProductSaleRankingVO) {
        //查看活动期产品销量排名
        //效验
        if (BeanUtil.isEmpty(activityProductSaleRankingVO)) {
            logger.info("传入参数为空");
            throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
        }
        if (StringUtils.isEmpty(activityProductSaleRankingVO.getActivityId())) {
            logger.info("必填项为空");
            throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
        }
        ActivityProductSaleRankingListBO activityProductSaleRankingListBO = new ActivityProductSaleRankingListBO();
        if (StringUtils.isEmpty(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserJobNumber())) {
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        } else {
            activityProductSaleRankingVO.setUserJobNumber(UserDetailUtil.getUserDetail().getUserJobNumber());
        }
        //获取机构编码
        String organizationCodes = activityAchievementMapper.getOrganizationCode(activityProductSaleRankingVO.getUserJobNumber());
        if (StringUtils.isEmpty(organizationCodes)) {
            throw new BaseException(BaseErrorEnum.USER_NOT_EXISTS);
        }
        String[] organizationCode = organizationCodes.substring(1).split(",");
        //设置排行数据
        BeanUtil.copyProperties(activityAchievementMapper.getActivityProductSaleRanking(activityProductSaleRankingVO, organizationCode), activityProductSaleRankingListBO);
        //设置产品排行
        activityProductSaleRankingListBO.setList(activityAchievementMapper.getActivityAllProductSaleRanking(activityProductSaleRankingVO, organizationCode, RANK));
        return activityProductSaleRankingListBO;
    }

}
